<template>
  <div class="resultContainer">
    <div class="search-result-box">
      <van-nav-bar title="搜索结果" left-arrow @click-left="$router.go(-1)" fixed></van-nav-bar>
    </div>
    <div class="resultBox">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :immediate-check="false"
        offset="10"
        @load="onLoad"
      >
        <ArticleItem  v-for="item in searchResults" :key="item.art_id" :articleDetail="item"></ArticleItem>
      </van-list>
    </div>
  </div>
</template>

<script>
import { getSearchResultAPI } from '@/api'
import ArticleItem from '@/components/ArticleItem'
export default {
  components: { ArticleItem },
  data () {
    return {
      searchResults: [],
      page: 1,
      per_page: 10,
      loading: false,
      finished: false
    }
  },
  created () {
    this.getSearchResultList()
  },
  methods: {
    async getSearchResultList () {
      const res = await getSearchResultAPI({ q: this.$route.params.keyword, page: this.page, per_page: this.per_page })
      if (res.data.data.results.length === 0) {
        this.finished = true
      }
      this.searchResults = [...this.searchResults, ...res.data.data.results]
    },
    onLoad () {
      if (this.searchResults.length > 0) {
        this.page++
        this.getSearchResultList()
        this.loading = false
        this.finished = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .search-result-box {
    padding-top: 46px;
  }
  .van-nav-bar {
    background-color: pink;
  }
  /deep/ .van-nav-bar__title {
    color: #fff;
  }
  /deep/ .van-icon-arrow-left {
    color: #fff;
  }
</style>
